<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>信息列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">



    <style>

        /* .layui-layer-title{
          tex;
        } */
        body{
          background-color:#ffffff;
        }
        .layui-table{
          /* text-align: center; */
          background-color: #ffffff;
        }
      .layui-table-cell {
           height: 60px;
          line-height: 60px;  
          /* height: auto !important; */
            white-space: normal;
            padding: 0px;
        }
    
        th .layui-table-cell { 
           height: 30px; 
         line-height: 30px; 
       }
    
        th .laytable-cell-1-0-7 {
          height: 30px;
          line-height: 30px;
        }
    
         .layui-table img {
          width: 100px;
          height: 60px;
        } 
      </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form id="form"  class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">员工名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="employname" placeholder="请输入用户名" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">性别</label>
                            <div class="layui-input-inline">
                                <input type="text" name="gender" placeholder="请输入性别" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">入职时间</label>
                            <div class="layui-input-inline">
                                <input type="text" name="hiredate" placeholder="请输入入职时间" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <label class="layui-form-label">工资</label>
                            <div class="layui-input-inline">
                                <input type="text" name="salary" placeholder="请输入工资" class="layui-input">
                            </div>
                        </div>

                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="allDelete"> 批量删除 </button>
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="import"> 批量导入 </button>
              </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <script type="text/html" id="picTpl">
     
            <!-- 这里的 checked 的状态只是演示 -->
            <img src="{{d.photo}}" name="photo"  alt="">
          </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script>
    layui.use(['form', 'table','upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload = layui.upload;
        table.render({
            elem: '#currentTableId',
            url: 'http://localhost:8080/employee/pageData',
            toolbar: '#toolbarDemo',

            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox" },
                {field: 'id',title: '序号' ,sort: true},
                {field: 'employname', title: '用户名'},
                {field: 'gender', title: '性别'},
                {field: 'age', title: '年龄',width:66},
                {field: 'phone', title: '手机号'},
                {field: 'hiredate', title: '入职时间',},
                {field: 'deptime', title: '离职时间' , templet : function(d){
                   var e = d.deptime; 
                  
                  if(d.deptime == null){
                      return "在职";
                    }else{
                      return e;
                    }
                    

                }},
                {field: 'salary', title: '工资'},
                {field: 'photo', title: '照片',templet: '#picTpl'},
                {title: '操作', toolbar: '#currentTableBar', align: "right"}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {

            var result = JSON.stringify(data.field)
                layer.alert(result,{
                  title: '最终的搜索信息'
           });

         //执行搜索重载
        table.reload('currentTableId', {
          page: {
            curr: 1
          }
          , where: {
            //searchParams: result
            "employname": data.field.employname,
            "gender": data.field.gender,
            "hiredate": data.field.hiredate,
            "salary": data.field.salary
          }
        }, 'data');

        return true;
      });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
          var checkStatus = table.checkStatus(obj.config.id);
          
          if(obj.event === 'import'){
            var index = layer.open({
                    title: '批量添加员工',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['19%', '50%'],
                    content: './adminupload.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
          
         }  else if(obj.event === 'allDelete'){
            var data=checkStatus.data;                 
                      var arr=[];
                    if(data.length!=0){
                     
                      for(var i of data){
                        if(checkStatus = true){
                          arr.push(i.id);
                        }else{
                          arr.pop(i.id);
                        }
                      }
                      var token = JSON.parse(sessionStorage.getItem("token"));
                      // var info = {token:token,
                      // id:arr
                      // }
                      var info = {
                      id:arr
                      }
                      console.log(info);
                      layer.confirm('确定删除id:'+ JSON.stringify(info).substring(JSON.stringify(info).indexOf("[")+1,JSON.stringify(info).lastIndexOf("]")) +'的数据吗?', function (index) {
                      $.ajax({
                        "url":"http://localhost:8080/employee/allDelete",
                        // method:'post',
                        "data": {"id": JSON.stringify(info).substring(JSON.stringify(info).indexOf("[")+1,JSON.stringify(info).lastIndexOf("]"))},
                        "success" : function(result){
                          if (result.code == 200) {
                                layer.msg("删除成功");
                                window.location.reload()
                                obj.del();
                             
                                  } else {
                          layer.msg("删除失败");
                                }
                            }                    
                        });
                      });
                      }else{
                        layer.msg("请选择删除的行")
                      }     
          } else if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加员工',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '100%'],
                    content: './adminadd.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }else if (obj.event === 'export') {  // 监听导出操作
                location.href="/api/admin/download?"+$("#form").serialize();
            }
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
  ;
                localStorage.setItem("admin",JSON.stringify(data));

                var index = layer.open({
                    title: '修改用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['80%', '100%'],
                    content: './adminedit.html',
                    end:function(index){
                        table.reload('currentTableId');
                    }
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除该行么', function (index) {
                    //请求接口实现数据库的删除，通过id
                    $.ajax({
              "url": "http://localhost:8080/employee/delete"
              ,"data": {
                "id": data.id
              },
              "success": function (result) {
                if (result.code == 200) {
                  layer.msg("删除成功");
                  obj.del();
                } else {
                  layer.msg("删除失败");
                }
                layer.close(index);
              }
            });
          //正如你看到的，每一种弹层调用方式，都会返回一个index
        layer.close(index); //此时你只需要把获得的index，轻轻地赋予layer.close即可

          });
            }
        });

    });
</script>

</body>
</html>